<template>
    <basic-container>
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   v-model:page="page"
                   v-model:search="search"
                   :permission="permissionList"
                   :before-open="beforeOpen"
                   v-model="form"
                   ref="crud"
                   @row-update="rowUpdate"
                   @row-save="rowSave"
                   @row-del="rowDel"
                   @sort-change="sortChange"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @refresh-change="refreshChange"
                   :row-class-name="rowClassName"
                   :cell-class-name="rowClassName"
                   @on-load="onLoad">
            <template #menu="{ row, size, type }">
                <el-tooltip content="送货完成" placement="top">
                    <el-button
                            icon="Check"
                            link
                            :size="size"
                            :type="type"
                            @click="() => closeData(row)"
                            v-if="row.deliComplete ==='否' || row.deliComplete ===''"
                    >
                    </el-button>
                </el-tooltip>

                <el-tooltip content="撤回" placement="top">
                    <el-button
                            icon="RefreshLeft"
                            link
                            :size="size"
                            :type="type"
                            @click="() => backData(row)"
                            v-if="row.deliComplete ==='是' && row.deliCompDate !=='' && row.deliCompOpr !==''"
                    >
                    </el-button>
                </el-tooltip>
            </template>
        </avue-crud>
    </basic-container>
</template>

<script>
    import {purchaseTracking,updateDeliComplete} from "@/api/anwise/purchase/purchase_item";
    import {mapGetters} from "vuex";
    export default {
        data() {
            return {
                isEnglish:'1',
                hiddenPrice:'1',
                selectId:'',
                box:false,
                form: {},
                query: {},
                loading: true,
                dateFormat: 'YYYY-MM-DD',
                pickerOptions: {},
                page: {
                    pageSize: 20,
                    currentPage: 1,
                    total: 0
                },
                search:{},
                selectionList: [],
                option: {
                    height:'auto',
                    calcHeight: 30,
                    tip: false,
                    searchShow: false,
                    searchMenuSpan: 6,
                    border: true,
                    index: true,
                    selection: true,
                    reserveSelection:true,
                    labelWidth: 150,
                    indexWidth:100,
                    menuWidth: 100,
                    addBtn: false,
                    editBtn: false,
                    viewBtn: false,
                    delBtn: false,
                    stripe: true,
                    searchIndex: 3,
                    searchLabelWidth: 120,
                    searchIcon: false,
                    searchEnter:true,
                    // selectable:(row,index)=>{
                    //   return row.orderStatus =='open';
                    // },
                    column: [
                        {
                            label: "采购单编号",
                            prop: "idNo",
                            type: "input",
                            fixed: true,
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:130,
                            rules: [{
                                required: true,
                                message: "请输入采购单编号",
                                trigger: "blur"
                            }],
                        },
                        {
                            label: "采购项编号",
                            prop: "itemNo",
                            type: "number",
                            precision:0,
                            min:1,
                            controls:false,
                            fixed: true,
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:130,
                            rules: [{
                                required: true,
                                message: "请输入采购项编号",
                                trigger: "blur"
                            }],
                        },
                        {
                            label: "供应商编号",
                            prop: "supNo",
                            type: "number",
                            precision:0,
                            min:0,
                            controls:false,
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:150,
                        },
                        {
                            label: "供应商名称",
                            prop: "supName",
                            type: "input",
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:150,
                        },
                        {
                            label: "所属机构账号",
                            prop: "deptNo",
                            type: "number",
                            precision:0,
                            min:0,
                            controls:false,
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:150,
                        },
                        {
                            label: "所属机构名称",
                            prop: "deptName",
                            type: "input",
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:150,
                        },
                        // {
                        //     label: "版本",
                        //     prop: "version",
                        //     type: "input",
                        //     overHidden: true,
                        //     sortable: true,
                        //     width:100,
                        // },
                        {
                            label: "需求id",
                            prop: "requireId",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:130,
                            rules: [{
                                required: true,
                                message: "请输入需求id",
                                trigger: "blur"
                            }],
                        },
                        {
                            label: "物料编号",
                            prop: "materialNo",
                            type: "input",
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:130,
                            rules: [{
                                required: true,
                                message: "请输入物料编号",
                                trigger: "blur"
                            }],
                        },
                        {
                            label: "物料名称",
                            prop: "materialName",
                            type: "input",
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:130,
                        },
                        {
                            label: "数量",
                            prop: "num",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:130,
                        },
                        {
                            label: "送货已完成数量",
                            prop: "deliNum",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:160,
                        },
                        {
                            label: "送货是否已完成",
                            prop: "deliComplete",
                            type: "select",
                            dicUrl: '/api/blade-system/dict-biz/dictionary?code=biz_yes_no',
                            props: {
                                label: 'dictValue',
                                value: 'dictKey',
                            },
                            dataType: 'string',
                            search: true,
                            overHidden: true,
                            sortable: true,
                            width:160,
                        },
                        {
                            label: "关闭人员",
                            prop: "deliCompOpr",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:160,
                        },
                        {
                            label: "关闭日期",
                            prop: "deliCompDate",
                            type: "input",
                            overHidden: true,
                            sortable: true,
                            width:160,
                        },
                    ]
                },
                data: []
            };
        },
        computed: {
            ...mapGetters(["permission"]),
            permissionList() {
                return {
                    addBtn: this.validData(this.permission.order_add, false),
                    viewBtn: this.validData(this.permission.order_view, false),
                    delBtn: this.validData(this.permission.order_delete, false),
                    editBtn: this.validData(this.permission.order_edit, false)
                };
            },
            ids() {
                let ids = [];
                this.selectionList.forEach(ele => {
                    ids.push(ele.id);
                });
                return ids.join(",");
            }
        },
        methods: {
            closeData(row){
                row.deliComplete = "是";
                updateDeliComplete(row).then(res=>{
                    if(res.data.success){
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                    }
                })
            },
            backData(row){
                row.deliComplete = "否";
                updateDeliComplete(row).then(res=>{
                    if(res.data.success){
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                    }
                })
            },
            rowClassName({row, rowindex}) {
                // if (row.orderStatus ==='open') {
                //     return 'row-yellow';
                // }
            },
            rowSave(row, done, loading) {
                add(row).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    done();
                }, error => {
                    loading();
                    window.console.log(error);
                });
            },
            rowUpdate(row, index, done, loading) {
                update(row).then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    done();
                }, error => {
                    loading();
                    window.console.log(error);
                });
            },
            rowDel(row) {
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        return remove(row.id);
                    })
                    .then(() => {
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                    });
            },
            handleDelete() {
                if (this.selectionList.length === 0) {
                    this.$message.warning("请选择至少一条数据");
                    return;
                }
                this.$confirm("确定将选择数据删除?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        return remove(this.ids);
                    })
                    .then(() => {
                        this.onLoad(this.page);
                        this.$message({
                            type: "success",
                            message: "操作成功!"
                        });
                        this.$refs.crud.toggleSelection();
                    });
            },
            beforeOpen(done, type) {
                if (["edit", "view"].includes(type)) {
                    getDetail(this.form.id).then(res => {
                        this.form = res.data.data;
                    });
                }
                done();
            },
            sortChange(val) {
                console.log(val)
                this.sort = {
                    prop: val.prop,
                    order: val.order
                };
                let order = "";
                if (this.sort.prop && this.sort.order) {
                    if (this.sort.order === "descending") {
                        order = `${this.sort.prop} desc`
                    } else {
                        order = `${this.sort.prop} asc`
                    }
                }
                this.query.orderBy = order;
                this.onLoad(this.page);
            },
            searchReset() {
                this.query = {};
                this.search.salesDateSearch = "";
                this.search.salesDateRule = "";
                this.search.customerNo = "";
                this.search.createTimeSearch = "";
                this.search.createTimeRule = "";
                this.onLoad(this.page);
            },
            searchChange(params, done) {
                this.query = params;
                this.page.currentPage = 1
                this.onLoad(this.page, params);
                done();
            },
            selectionChange(list) {
                this.selectionList = list;
            },
            selectionClear() {
                this.selectionList = [];
                this.$refs.crud.toggleSelection();
            },
            currentChange(currentPage){
                this.page.currentPage = currentPage;
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize;
            },
            refreshChange() {
                this.onLoad(this.page, this.query);
            },
            onLoad(page, params = {}) {
                this.loading = true;
                purchaseTracking(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                    const data = res.data.data;
                    this.page.total = data.total;
                    this.data = data.records;
                    this.loading = false;
                    this.selectionClear();
                });
            }
        }
    };
</script>

<style>
    .row-yellow {
        background-color: rgb(255, 253, 230) !important;
        cursor: pointer;
    }

    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button{
        -webkit-appearance: none !important;
        margin: 0;
    }
</style>
